package com.rxyb.quartz.controller;

import com.rxyb.quartz.config.RspResult;
import com.rxyb.quartz.controller.vo.QuartzBean;
import com.rxyb.quartz.service.IJobService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/job")
@Slf4j
public class JobController {
    private final static Logger LOGGER = LoggerFactory.getLogger(JobController.class);

    @Autowired
    private Scheduler scheduler;
    @Autowired
    private IJobService jobService;

    @PostMapping("/add")
    public RspResult save(QuartzBean quartz){
        LOGGER.info("新增任务");
        try {
            jobService.save(quartz);
        } catch (Exception e) {
            e.printStackTrace();
            return RspResult.error();
        }
        return RspResult.ok();
    }
    @PostMapping("/list")
    public RspResult list(QuartzBean quartz,Integer pageNo,Integer pageSize) throws SchedulerException {
        LOGGER.info("任务列表");
        return jobService.listQuartzEntity(quartz, pageNo, pageSize);
    }
    @PostMapping("/trigger")
    public  RspResult trigger(QuartzBean quartz, HttpServletResponse response) {
        LOGGER.info("触发任务");
        try {
            JobKey key = new JobKey(quartz.getJobName(),quartz.getJobGroup());
            scheduler.triggerJob(key);
        } catch (SchedulerException e) {
            e.printStackTrace();
            return RspResult.error();
        }
        return RspResult.ok();
    }
    @PostMapping("/pause")
    public  RspResult pause(QuartzBean quartz,HttpServletResponse response) {
        LOGGER.info("停止任务");
        try {
            JobKey key = new JobKey(quartz.getJobName(),quartz.getJobGroup());
            scheduler.pauseJob(key);
        } catch (SchedulerException e) {
            e.printStackTrace();
            return RspResult.error();
        }
        return RspResult.ok();
    }
    @PostMapping("/resume")
    public  RspResult resume(QuartzBean quartz,HttpServletResponse response) {
        LOGGER.info("恢复任务");
        try {
            JobKey key = new JobKey(quartz.getJobName(),quartz.getJobGroup());
            scheduler.resumeJob(key);
        } catch (SchedulerException e) {
            e.printStackTrace();
            return RspResult.error();
        }
        return RspResult.ok();
    }
    @PostMapping("/remove")
    public  RspResult remove(QuartzBean quartz,HttpServletResponse response) {
        LOGGER.info("移除任务");
        try {
            TriggerKey triggerKey = TriggerKey.triggerKey(quartz.getJobName(), quartz.getJobGroup());
            // 停止触发器
            scheduler.pauseTrigger(triggerKey);
            // 移除触发器
            scheduler.unscheduleJob(triggerKey);
            // 删除任务
            scheduler.deleteJob(JobKey.jobKey(quartz.getJobName(), quartz.getJobGroup()));
            System.out.println("removeJob:"+JobKey.jobKey(quartz.getJobName()));
        } catch (Exception e) {
            e.printStackTrace();
            return RspResult.error();
        }
        return RspResult.ok();
    }
}
